package com.guzipi.websocketdemo.config;

import com.guzipi.websocketdemo.bean.MyHandler;
import com.guzipi.websocketdemo.bean.MyHandlerCopy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myHandler(), "/myHandler/*")//*位置传递用户id
                .addHandler(myHandlerCopy(), "/multiWebsocketApi/*")
                .addInterceptors(new HttpSessionHandshakeInterceptor()).
                setAllowedOrigins("*");
    }
    @Bean
    public WebSocketHandler myHandler() {
        return new MyHandler();
    }
    @Bean
    public WebSocketHandler myHandlerCopy() {
        return new MyHandlerCopy();
    }
}
